package com.itheima.test05;
/*
二分查找
 */
public class Test03 {

    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9,10};
        int num=0;
        int index = getIndex(arr, num);

        System.out.println(index);


    }

    public static int getIndex(int[] arr, int num) {
        int index=-1;

        //定义两个变量   start 从0开始
        int  start=0;
        //                        end 从length-1开始
        int end=arr.length-1;
        //死循环{
        while (start<=end){
            //获取到mid mid=(start+end)/2
            int mid=(start+end)/2;
            //比较 arr[mid]   num
            if(num>arr[mid]){
                //如果大了  start=mid+1
                start=mid+1;
            }else if(num<arr[mid]){//如果小了 end= mid-1
                end=mid-1;
            }else{ //相等    找到了 index=mid
                index=mid;
                break;
            }

        }
        return index;
    }
}
